class Solution:
    def longestConsecutive(self, nums: list) -> int:
        if len(nums) == 0:
            return 0
        nums.sort()
        res, mid_res, mid_num = 0, 1, nums[0]
        iterer = iter(nums)
        try:
            while True:
                mid_num2 = next(iterer)
                if mid_num2 == mid_num:
                    continue
                elif mid_num2 == mid_num + 1:
                    mid_res += 1
                else:
                    res = max(mid_res, res)
                    mid_res = 1
                mid_num = mid_num2
        except:
            res = max(mid_res, res)
            return res

    def longestConsecutive2(self, nums: list) -> int:
        if len(nums) == 0:
            return 0
        mid_dic, res = {}, 0
        for dec in nums:
            if dec in mid_dic:
                continue
            left = mid_dic.get(dec - 1, 0)
            right = mid_dic.get(dec + 1, 0)
            mid_num = 1 + left + right
            mid_dic[dec] = mid_num
            mid_dic[dec-left] = mid_num
            mid_dic[dec+right] = mid_num
            res = max(res, mid_num)
        return res



a = Solution()
print(a.longestConsecutive2([100, 4, 200, 1, 3, 2]))
print(a.longestConsecutive2([1, 2, 0, 1]))
print(a.longestConsecutive2([100, 4, 200, 1, 3, 2, 201, 202, 203, 204]))
print(a.longestConsecutive2([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))
